package com.house.rent.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.base.lib.mvp.present.BasePresenter;
import com.house.rent.R;
import com.house.rent.entity.UserEntity;
import com.house.rent.ui.adapter.IdCardAdapter;

import java.util.List;

import butterknife.BindView;

/**
 * @desc 待审核列表
 */
public class CheckIdCardActivity extends BaseActivity {

    @BindView(R.id.refresh_check_idCard)
    SwipeRefreshLayout mSwipeRefreshLayout;
    @BindView(R.id.rcv_check_idCard)
    RecyclerView mRecyclerView;

    IdCardAdapter mAdapter;

    public static void start(Context context) {
        Intent starter = new Intent(context, CheckIdCardActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_check_id_card;
    }

    @Override
    protected void initTop() {
        setTitle("待审核列表");
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mSwipeRefreshLayout.setColorSchemeColors(ContextCompat.getColor(mContext, R.color.colorPrimary));

        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mRecyclerView.setAdapter(mAdapter = new IdCardAdapter(mContext));
    }

    @Override
    protected void initListeners() {
        mSwipeRefreshLayout.setOnRefreshListener(() -> {
            mSwipeRefreshLayout.postDelayed(() -> hideLoading(), 1500);
        });
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            IdCardDetailActivity.start(mContext, mAdapter.getData().get(position).getId(), true);
        });
        mAdapter.setOnLoadMoreListener(null, mRecyclerView);
    }

    @Override
    protected BasePresenter initPresenter() {
        return null;
    }

    @Override
    protected void initData() {
        showLoading("正在获取列表...");
        sendRequest("user/auditList", UserEntity.class);
    }

    @Override
    protected void onRequestSuccess(String reqId, Object obj) {
        super.onRequestSuccess(reqId, obj);
        if (obj instanceof List) {
            List<UserEntity> list = (List<UserEntity>) obj;
            mAdapter.setNewData(list);
            mAdapter.loadMoreEnd();
        }
    }

    @Override
    public void showLoading(String message) {
        if (!mSwipeRefreshLayout.isRefreshing()) {
            super.showLoading(message);
        }
    }

    @Override
    public void hideLoading() {
        super.hideLoading();
        if (mSwipeRefreshLayout.isRefreshing()) {
            mSwipeRefreshLayout.setRefreshing(false);
        }
    }

}
